import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author Yin_JC
 * @PackageName:PACKAGE_NAME
 * @Class:JDBCTest
 * @Description:
 * @date 2020/3/5 12:22
 */

public class JDBCTest {
    @Test
    public void test1() throws SQLException {
        Driver driver = new com.mysql.jdbc.Driver();
        String url = "jdbc:mysql://localhost:3306/ceshi?useUnicode=true&characterEncoding=utf-8&useSSL=false";
        Properties info = new Properties();
        info.setProperty("user","root");
        info.setProperty("password","82191817");
        Connection connection = driver.connect(url,info);
        System.out.println(connection);
    }

    @Test
    public void test2() throws SQLException, ClassNotFoundException {
        String url = "jdbc:mysql://localhost:3306/ceshi?useUnicode=true&characterEncoding=utf-8&useSSL=false";
        String user = "root";
        String password = "82191817";
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection(url,user,password);
        System.out.println(connection);
    }


    //读取配置文件
    @Test
    public void getConnection() throws IOException, ClassNotFoundException, SQLException {
        //读取配置文件信息
        InputStream resourceAsStream = JDBCTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties pros = new Properties();
        pros.load(resourceAsStream);
        String user = pros.getProperty("user");
        String password = pros.getProperty("password");
        String url = pros.getProperty("url");
        String driverClass = pros.getProperty("driverClass");

        Class.forName(driverClass);
        Connection connection = DriverManager.getConnection(url,user,password);
        System.out.println(connection);
    }
}
